import { ref } from 'vue'
// @ts-ignore
import { downloadFn, byUrlDownload } from '@/utils/exportFile'
type CallbackFn = (item?: any) => void
export const usePageTable: any = (
     newHandleCallback?: CallbackFn,
     editHandleCallback?: CallbackFn,
     queryHandleCallback?: CallbackFn,
     delHandleCallback?: CallbackFn,
) => {
     const pageModalRef = ref()
     const pageContentRef = ref()
     const pageSearchRef = ref()
     const permissionRef: any = ref({
          permissionsAdd: false,
          permissionsEdit: false,
          permissionsDelete: false,
          permissionsDeleteAll: false,
          permissionsExport: false,
          permissionsUpload: false,
          permissionsTemplate: false,
          permissionsQuery: false,
     })
     const searchFormConfigRef = ref()
     const contentTableConfigRef = ref()
     const modalConfigRef = ref()
     const modalInfo: any = ref({})
     const tableLoading = ref(false)
     const selectCheckData = ref([])

     const handleOnSuccess = () => {
          pageContentRef.value!.getPageData()
     }
     const handleNewData = (item: any = {}) => {
          console.log(item, '新增')
          item.modalType = 'add'
          modalInfo.value = item
          pageModalRef.value!.dialogVisible = true

          newHandleCallback && newHandleCallback()
     }
     const handleEditData = (item: any) => {
          item.modalType = 'edit'
          modalInfo.value = item
          pageModalRef.value!.dialogVisible = true
          editHandleCallback && editHandleCallback(item)
     }
     const handleDelData = async (item: any) => {
          console.log('删除', item)
          modalInfo.value = item
          delHandleCallback && (await delHandleCallback(item))
          // pageContentRef.value!.getPageData()
     }
     //查询
     const handleQueryClick = (data: any) => {
          console.log(data, '查询')
          pageContentRef.value!.getPageData(data)
          queryHandleCallback && queryHandleCallback(data)
     }
     // 点击重置
     const handleResetClick = (data: any) => {
          console.log(pageContentRef.value, '点击重置')
          pageContentRef.value.pageInfo.offset = 1
          pageContentRef.value.pageInfo.pagesize = 10
          pageContentRef.value!.getPageData(data)
     }

     const getSelectionData = (data: any) => {
          console.log(data, '选中')
          selectCheckData.value = data
     }
     const templateDownloadClick = (url: any, fileName: any) => {
          console.log('模板下载')
          byUrlDownload(url, fileName)
     }
     // 判断权限
     const hasPermission = (data: any) => {
          permissionRef.value = {
               permissionsAdd: false,
               permissionsEdit: false,
               permissionsDelete: false,
               permissionsDeleteAll: false,
               permissionsExport: false,
               permissionsUpload: false,
               permissionsTemplate: false,
               permissionsQuery: false,
               getCurveType: false,
          }
          data.forEach((item: any) => {
               permissionRef.value[item] = true
          })
     }
     return {
          searchFormConfigRef,
          contentTableConfigRef,
          modalConfigRef,
          pageModalRef,
          pageContentRef,
          pageSearchRef,
          modalInfo,
          tableLoading,
          selectCheckData,
          permissionRef,
          handleOnSuccess,
          handleNewData,
          handleEditData,
          handleDelData,
          handleQueryClick,
          handleResetClick,
          getSelectionData,
          templateDownloadClick,
          hasPermission,
     }
}
